package com.qianbao.qianbaobusiness.ui.support.wechatprogram;

import android.text.TextUtils;

import com.androidbase.base.mvp.RxMvpPresenter;
import com.androidbase.utils.RxUtils;
import com.baseandroid.config.Global;
import com.baseandroid.config.MyApi;
import com.baseandroid.retrofit.RxObserver;
import com.baseandroid.retrofit.json.Data;
import com.qianbao.qianbaobusiness.json.MerchantInfo;
import com.qianbao.qianbaobusiness.json.WechatAppExistsBean;
import com.qianbao.qianbaobusiness.json.WechatAppQrBean;

import java.util.List;

import javax.inject.Inject;

/**
 * @author: Routee
 * @date 2018/6/20
 * @mail wangc4@qianbaocard.com
 * ------------1.本类由Routee开发,阅读、修改时请勿随意修改代码排版格式后提交到git。
 * ------------2.阅读本类时，发现不合理请及时指正.
 * ------------3.如需在本类内部进行修改,请先联系Routee,若未经同意修改此类后造成损失本人概不负责。
 */
public class WechatProgramPresenter extends RxMvpPresenter<WechatProgramView> {
    MyApi mApi;

    @Inject
    public WechatProgramPresenter(MyApi api) {
        mApi = api;
    }

    public void checkWechatQrcode() {
        mApi.checkWechatQrcode(getMerchantListString()).compose(RxUtils.applySchedulersLifeCycle(getView()))
                .subscribe(new RxObserver<Data<WechatAppExistsBean>>() {
                    @Override
                    public void onNext(Data<WechatAppExistsBean> data) {
                        super.onNext(data);
                        if (RxObserver.checkJsonCode(data, false)) {
                            getView().checkQrcodeUI(data.getResult().isIssigned());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }

    public void getWechatQrcode(String merchantNo) {
        mApi.getWechatQrcode(merchantNo).compose(RxUtils.applySchedulersLifeCycle(getView()))
                .subscribe(new RxObserver<Data<WechatAppQrBean>>() {
                    @Override
                    public void onNext(Data<WechatAppQrBean> data) {
                        super.onNext(data);
                        if (RxObserver.checkJsonCode(data, false)) {
                            getView().updateQrcodeUi(data.getResult().getQrcode());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }

    public void creatQrcode(String merchantNo) {
        mApi.creatQrcode(getMerchantListString(), merchantNo).compose(RxUtils.applySchedulersLifeCycle(getView()))
                .subscribe(new RxObserver<Data<WechatAppQrBean>>() {
                    @Override
                    public void onNext(Data<WechatAppQrBean> data) {
                        super.onNext(data);
                        if (RxObserver.checkJsonCode(data, false)) {
                            getView().updateQrcodeUi(data.getResult().getQrcode());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }

    private String getMerchantListString() {
        StringBuilder sb = new StringBuilder();
        List<MerchantInfo> merchantInfos = Global.getWechatAppInfos();
        for (MerchantInfo info : merchantInfos) {
            sb.append(info.merchantNo).append(",");
        }
        String result = "";
        if (!TextUtils.isEmpty(sb)) {
            result = sb.subSequence(0, sb.lastIndexOf(",")).toString();
        }
        return result;
    }
}
